package cn.netjava.robo3d.event;

import java.util.List;

import cn.netjava.robo3d.TankInformation;

import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
/**
 * 在这里判断一坦克和其它坦克有无相撞
 * @author Administrator
 *
 */
public class ImpactCarEvent {

	private TankInformation info;
	private TankInformation impacttank;
	private List<TankInformation> allothertanksinfo;       //引用所有其它坦克
    private float Min=1f;       //碰撞的最小距离
    private float Max=2.5f;      //碰撞的最大距离
    private Box base1;
    private Box base2;
	private float forward;

	public ImpactCarEvent(TankInformation info,List<TankInformation> allothertanksinfo){
		this.info=info;
		this.allothertanksinfo=allothertanksinfo;
	}
	public Vector3f getdistance(){ //返回一个距离向量
    	Vector3f myloc=info.getLocaltranslation();
        Vector3f opploc=impacttank.getLocaltranslation();
        return myloc.subtract(opploc);
    }

	//返回一个横向和纵向速度
	   public float[] getimpactedspeed(){
		   Vector3f opp=impacttank.getCardirection().mult(impacttank.getSpeed());
		   Vector3f me=info.getCardirection().mult(info.getSpeed());
		   Vector3f relatespeed=opp.subtract(me).mult(0.4f);
		   forward=info.getCardirection().dot(relatespeed);
	
		   float left=-4f*info.getLeftdirection().dot(relatespeed);
		   float result[]={forward,left};
		   return result;
	   }

    public TankInformation getimpacttank(){
		return impacttank;
	}


   public boolean iscut(){//融合
	   boolean cut=false;
	   if(base1.hasTriangleCollision(base2)){
		   cut=true;
	   }
	   return cut;
   }

   public boolean ishappen(){
	boolean happen=false;
	 //假设每一很短时刻只与一个发生碰撞
	 Vector3f loc1=info.getLocaltranslation();
	 float length=1000;
	 for(int i=0;i<allothertanksinfo.size();i++){
		  Vector3f loc2=allothertanksinfo.get(i).getLocaltranslation();
		  length=loc1.distance(loc2);
			  if(length<=Max){ //这么近,撞了
		              happen=true;
		              impacttank=allothertanksinfo.get(i);
					  base1=info.getBase();
					  base2=allothertanksinfo.get(i).getBase();
		              return happen;
	            }
	 }
	return happen;
}
}
